<?php
namespace Admin\Model;

use Think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/12
 * Time: 16:51
 */
class AdminModel extends Model
{

    protected $connection = 'KEFU';
    protected $trueTableName = 'admin';
//    protected $patchValidate = true;
    protected $updateFields = array('pwd');
    protected $_validate = array(
        array('name', 'require', '管理员名不能为空'),
        array('phone', 'require', '账号不能为空'),
        array('phone', '', '该账户已存在', '0', 'unique','1'),
        array('pwd', 'require', '密码不能为空'),
        array('rid', 'require', '角色不能为空'),//7 是外包
        array('role_id', 'require', '部门不能为空'),
        array('agent', 'require', '坐席号'),
        array('out_kf_id', 'require', '外包公司ID'),// 0 是内部客服
    );

    //判断用户名和密码是否正确
    public function check_login($phone, $pwd)
    {
        $admin = $this->getByphone($phone);
        if (!empty($admin)) {
            if ($admin['pwd'] == $pwd && $admin['status'] == '1') {
                return $admin;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    //修改密码判断
    public function check_modify_pwd($phone, $old_pwd, $new_pwd)
    {
        $admin = $this->getByphone($phone);
        if (!empty($admin)) {
            if ($admin['pwd'] == $old_pwd) {
                if ($old_pwd != $new_pwd) {
                    return 1001;
                } else {
                    return 1003;
                }
            } else {
                return 1002;
            }
        } else {
            return 1004;
        }
    }

    public function getByphone($phone){
        $res=M('admin','','KEFU')->where(['phone'=>$phone])->find();
        return $res;
    }
}